#include "pic.h"
#include "asm.h"

void init_pic(int pic1, int pic2) {
	outb(MASTER, EOI);                      // Send EOI | resets the chip

	outb(MASTER, ICW1_INIT+ICW1_ICW4);      // ICW1 control word setup | just basic PIC stuff
	outb(SLAVE, ICW1_INIT+ICW1_ICW4);       // see pic.h for more details about the values

	outb(MASTERDATA, pic1);                 // ICW2 maps IRQs 0-7 to whatever kernel passes
	outb(SLAVEDATA, pic2);                  // and same here except with IRQs 8-15 

	outb(MASTERDATA, 0x04);                 // ICW3 
	outb(SLAVEDATA, 0x02);

	outb(MASTERDATA, ICW4_8086);            // ICW4 control word setup
	outb(SLAVEDATA, ICW4_8086);
} 

void enable_irq(int irq) {
  u32 irqmask;
 
  if(irq == 2)
    return;
  if (irq < 8) {
    irqmask = inb(MASTER);
    outb(MASTER, irqmask & ~(1<<irq));
  } else {
    irqmask = inb(SLAVE);
    outb(SLAVE, irqmask & ~(1<<(irq-8)));

  }
}

void disable_irq(int irq) {
  u32 irqmask;

  if (irq==2)
    return;
  if (irq < 8) {
    irqmask = inb(MASTER);
    outb(MASTER, irqmask | (1<<irq));
  } else {
    irqmask = inb(SLAVE);
    outb(SLAVE, irqmask | (1<<(irq-8)));
  }
}
